package com.gcd.tempe;

import org.json.JSONArray;
import org.json.JSONObject;

import com.gcd.tempe.util.GlobalVariabel;
import com.gcd.tempe.util.JSON_Response;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class LihatHistoryPesananActivity extends Activity {
	
	//	dummy for user name and password
	//private static String usernameDummy = "rezanachmad@gmail.com";
	//private static String passwordDummy = "rezan";
	
	public static String baseOperation = "http://10.28.11.233/Tempe/service/?operation=";
	
	public static String CONFIRM_ORDER = "pesanan.confirmPay&pesanan_id=";	//&username=[username]&password=[pass]
	public static String GET_ALL_HISTORY = "pesanan.confirmGetAll&username=";	//[username]&password=[pass]
	public static String GET_HISTORY_BY_STATUS = "pesanan.confirmGetAll&username=";	//[username]&password=[pass]&status=[nilainya 1, 2, 3 atau 4]
	/*
	private final static int SEMUA_ID = 0;
	private final static int BELUM_BAYAR_ID = 1;
	private final static int SUDAH_KONFIRM_ID = 2;
	private final static int SUDAH_BAYAR_ID = 3;
	private final static int SUDAH_TIBA_ID = 4;
	*/
	private final static String SEMUA_S = "Semua";
	private final static String BELUM_BAYAR_S = "Belum dibayar";
	private final static String SUDAH_KONFIRM_S = "Sudah konfirmasi bayar";
	private final static String SUDAH_BAYAR_S = "Sudah dibayar";
	private final static String SUDAH_TIBA_S = "Barang sudah tiba";
	
/*	private final static int[] STATUS_ID = new int[] {
		SEMUA_ID, BELUM_BAYAR_ID, SUDAH_KONFIRM_ID, SUDAH_BAYAR_ID, SUDAH_TIBA_ID
	};*/
	private final static String[] STATUS_S = new String[]{
		SEMUA_S, BELUM_BAYAR_S, SUDAH_KONFIRM_S, SUDAH_BAYAR_S, SUDAH_TIBA_S
	};
	
	private Integer currentStatusId = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lihat_historypesanan);
		setOtherViews();
	}
	
	private void setOtherViews(){
		//	set list category view
		setListCategory();
		//	set listener for refresh button
		setRefreshButtonListener();
	}
	
	private void setRefreshButtonListener() {
		Button button = (Button) findViewById(R.id.Refresh);
		button.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				System.gc();
				setContentHistory(JSON_Response.getAllHistory(GlobalVariabel.userLoginInfo.userName, GlobalVariabel.userLoginInfo.password, currentStatusId));
			}
		});
	}

	private void setListCategory(){
		Spinner spinner = (Spinner) findViewById(R.id.choose_status);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, STATUS_S);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				currentStatusId = position;
				setContentHistory(JSON_Response.getAllHistory(GlobalVariabel.userLoginInfo.userName, GlobalVariabel.userLoginInfo.password, currentStatusId));
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
			
		});
	}
	
	//	set content to content which is active
	private void setContentHistory(JSONArray jsonArray){
		LinearLayout ll = (LinearLayout) findViewById(R.id.wrapperbarang);
		ll.removeAllViewsInLayout();
		
		if (jsonArray != null){
			try {
				for(int i = 0; i < jsonArray.length(); ++i){
					final JSONObject jsonObject = jsonArray.getJSONObject(i);				
					
					
					LinearLayout lytContainer = new LinearLayout(this);
					lytContainer.setPadding(5, 5, 0, 5);
					lytContainer.setOrientation(LinearLayout.VERTICAL);
					if (i % 2 == 1) {
						lytContainer.setBackgroundColor(Color.WHITE);
					} else {
						lytContainer.setBackgroundColor(Color.DKGRAY);
					}
					
					TextView t = new TextView(this);
					t.setText("No order : " + jsonObject.getString("id"));
					if (i % 2 == 1) {
						t.setTextColor(Color.BLACK);
					} else {
						t.setTextColor(Color.WHITE);
					}
					lytContainer.addView(t);
					
					TextView t2 = new TextView(this);
					t2.setText("Tanggal : " + jsonObject.getString("tanggal"));
					if (i % 2 == 1) {
						t2.setTextColor(Color.BLACK);
					} else {
						t2.setTextColor(Color.WHITE);
					}
					lytContainer.addView(t2);
					
					TextView t3 = new TextView(this);
					t3.setText("Status : " + STATUS_S[jsonObject.getInt("status")]);
					if (i % 2 == 1) {
						t3.setTextColor(Color.BLACK);
					} else {
						t3.setTextColor(Color.WHITE);
					}
					lytContainer.addView(t3);
					
					lytContainer.setOnClickListener(new OnClickListener() {
						
						public void onClick(View arg0) {
							try {
								System.gc();
								Intent intent = new Intent(LihatHistoryPesananActivity.this, LihatItemHistoryPesananActivity.class);
								intent.putExtra("orderid", jsonObject.getString("id"));
								intent.putExtra("orderdate", jsonObject.getString("tanggal"));
								intent.putExtra("orderstatus", jsonObject.getString("status"));
								intent.putExtra("orderitems", jsonObject.getString("pesananItems"));
								intent.putExtra("username", GlobalVariabel.userLoginInfo.userName);
								intent.putExtra("password", GlobalVariabel.userLoginInfo.password);
								startActivity(intent);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
					ll.addView(lytContainer);
				}
				
			}catch (Exception e) {
				e.printStackTrace();
			}
		} else{
			try {
				LinearLayout lytContainer = new LinearLayout(this);
				lytContainer.setOrientation(LinearLayout.VERTICAL);
				
				TextView t = new TextView(this);
				String message = "Tidak ditemukan pesanan";
				if (currentStatusId != 0){
					message += " dengan status " + STATUS_S[currentStatusId];
				}
				t.setText(message);
				t.setTextColor(Color.BLACK);
				lytContainer.addView(t);
				ll.addView(lytContainer);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
	}
	
}
